package UI;

import facecat.topin.core.*;
import facecat.topin.btn.*;
import facecat.topin.tab.*;

public class RibbonButton extends FCButton {
    public RibbonButton()
    {
        setBackColor(FCColor.None);
        setBorderColor(FCColor.None);
        setFont(new FCFont("Default", 12, false, false, false));
    }

    private int m_arrowType;

    public boolean isSelected()
    {
        FCView parent = getParent();
        if (parent != null)
        {
            
            FCTabView tabView = (FCTabView)((parent instanceof FCTabView) ? parent : null);
            if (tabView != null)
            {
                FCTabPage selectedTabPage = tabView.getSelectedTabPage();
                if (selectedTabPage != null)
                {
                    if (this == selectedTabPage.getHeaderButton())
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public void onPaintBackground(FCPaint paint, FCRect clipRect)
    {
//        FCNative native = getNative();
        int width = getWidth(), height = getHeight();
        int mw = width / 2, mh = height / 2;
        FCRect drawRect = new FCRect(0, 0, width, height);
        int cornerRadius = 0;
        boolean isTabHeader = false;
        cornerRadius = 0;
        if (m_arrowType > 0)
        {
            cornerRadius = 0;
        }

        FCView parent = getParent();
        if (parent != null)
        {
            
            FCTabView tabView =(FCTabView)((parent instanceof FCTabView) ? parent : null);
            if (tabView != null)
            {
                cornerRadius = 0;
                isTabHeader = true;
            }
        }
        if (isTabHeader)
        {
            drawRect = new FCRect(drawRect.left, 1, drawRect.right, drawRect.bottom - 1);
            if (MyColor.m_style == 1)
            {
                if (isSelected())
                {
                    paint.fillRect(FCColor.rgb(150, 150, 150), drawRect);
                }
                else
                {
                    paint.fillRect(FCColor.rgb(200, 200, 200), drawRect);
                    paint.drawLine(FCColor.rgb(70, 76, 79), 1, 0, width - 3, height / 4, width - 3, height * 3 / 4);
                }
            }
            else
            {
                if (isSelected())
                {
                    paint.fillRect(FCColor.rgb(75, 75, 75), drawRect);
                }
                else
                {
                    paint.fillRect(FCColor.rgb(50, 50, 50), drawRect);
                    paint.drawLine(FCColor.rgb(70, 76, 79), 1, 0, width - 3, height / 4, width - 3, height * 3 / 4);
                }
            }
        }
        else
        {
            cornerRadius = 0;
            if (m_arrowType > 0)
            {
                FCPoint point1 = new FCPoint();
                FCPoint point2 = new FCPoint();
                FCPoint point3 = new FCPoint();
                int dSize = Math.min(mw, mh) / 2;
                switch (m_arrowType)
                {
                    case 1:
                        point1.x = mw - dSize;
                        point1.y = mh;
                        point2.x = mw + dSize;
                        point2.y = mh - dSize;
                        point3.x = mw + dSize;
                        point3.y = mh + dSize;
                        break;
                    case 2:
                        point1.x = mw + dSize;
                        point1.y = mh;
                        point2.x = mw - dSize;
                        point2.y = mh - dSize;
                        point3.x = mw - dSize;
                        point3.y = mh + dSize;
                        break;
                    case 3:
                        point1.x = mw;
                        point1.y = mh - dSize;
                        point2.x = mw - dSize;
                        point2.y = mh + dSize;
                        point3.x = mw + dSize;
                        point3.y = mh + dSize;
                        break;
                    case 4:
                        point1.x = mw;
                        point1.y = mh + dSize;
                        point2.x = mw - dSize;
                        point2.y = mh - dSize;
                        point3.x = mw + dSize;
                        point3.y = mh - dSize;
                        break;
                }
                FCPoint[] points = new FCPoint[3];
                points[0] = point1;
                points[1] = point2;
                points[2] = point3;
                paint.fillPolygon(FCColor.Text, points);
            }
            if (m_arrowType == 0)
            {
                boolean state = false;
                if (this == m_native.getPushedView())
                {
                    state = true;
                    paint.fillRoundRect(FCColor.rgba(255, 255, 255, 25), drawRect, cornerRadius);
                }
                else if (this == m_native.getHoveredView())
                {
                    state = true;
                    paint.fillRoundRect(FCColor.rgba(0, 0, 0, 25), drawRect, cornerRadius);
                }
                if (state)
                {
                    if (cornerRadius > 0)
                    {
                        paint.drawRoundRect(FCColor.rgb(255, 255, 255), 2, 0, drawRect, cornerRadius);
                    }
                    else
                    {
                        paint.drawRect(FCColor.rgb(255, 255, 255), 1, 0, drawRect);
                    }
                }
            }
        }
    }
}

